<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');
require_once('../../_inc/class.rglDocGen.php');

$sSql = "SELECT sd.* "
	." FROM rgl_set_dati sd "
	." WHERE sd.id_set_dati = ".reqVarInt('sd');
$objDB->query($sSql, 'readBil');
$aRec = $objDB->fetch('readBil');
if (!$aRec) {
	die('no bil. '.reqVarAS('ca')."/".reqVarAS('dr'));
}

$aTTD = array(1=>'Bilancio UE');
$aTTD[2] = 'Bilancio riclassificato';
$aTTD[3] = 'Rendiconto finanziario';
$aTTD[11] = $aRec['indici1'];
$aTTD[12] = $aRec['indici2'];
$aTTD[13] = $aRec['indici3'];

$aGeDati = getGerarcTabDati(reqvar('sd'),reqvar('ttd'));
//dbg_($aGeDati);
//dbg_($aDati);
$oDoc = new rglDocGen();
$oDoc->maxCol = 40;
$oDoc->pdfIntestaz = $aRec['cod_set_dati']." - ".$aRec['descr'];
$oDoc->bottomLeftText = date("d/m/Y H:i:s")." (".$_SERVER['HTTP_HOST'].RGL_URL_ROOT.")";
$oDoc->nuovaRigaNP();
$aCol = array('cla'=>'lab1','col'=>3,'alg'=>'C','txt'=>'Codice');
$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>12,'alg'=>'C','txt'=>'Descrizione');
$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Seq.');
$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Tipo');
$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Flag');
$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>19,'alg'=>'C','txt'=>'Script');
$oDoc->setColNP($aCol);

$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>$aTTD[reqVar('ttd')]);
$oDoc->setCol($aCol);
$oDoc->spaziatura();
$oDoc->forzaLabNP();

foreach($aGeDati as $codX => $aX) {
	genPdfDat($oDoc,$codX,$aX,0);
}
$sPdf = $oDoc->genPdf(array('orientaPagina'=>'L'));
headerDownLoad(strlen($sPdf),"tabella_dati_".$aRec['cod_def_tdati'].".pdf");
echo $sPdf;
exit();


function genPdfDat(&$oDoc,$codX, $aX, $liv) {
	global $objDB,$objSess;
	$sSql = "SELECT d.* "
		." FROM rgl_def_dato d "
		." WHERE d.cod_dato = '$codX'"
		;
	$objDB->query($sSql, 'readDato');
	$acD = $objDB->fetch('readDato');
	$oDoc->nuovaRiga();
	$oDoc->setParRiga('labNP',true);
	$aCol = array('cla'=>'txt1','col'=>3,'alg'=>'L','txt'=>$codX);
	$oDoc->setCol($aCol);
	$txt = str_repeat(' ', $liv*4).$acD['descr'];
	$aCol = array('cla'=>'txt1','col'=>12,'alg'=>'L','txt'=>$txt);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>2,'alg'=>'R','txt'=>$acD['seq']);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>2,'alg'=>'C','txt'=>$acD['tipo_valore']);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>2,'alg'=>'C','txt'=>$acD['flag_quad_calc']);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>19,'alg'=>'L','txt'=>$acD['script']);
	$oDoc->setCol($aCol);
	foreach($aX['figli'] as $codY => $aY)  {
		genPdfDat($oDoc,$codY, $aY, $liv+1);
	}
}

function zzRigaLab(&$oDoc) {
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'lab1','col'=>3,'alg'=>'C','txt'=>'Codice');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>12,'alg'=>'C','txt'=>'Descrizione');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Seq.');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Tipo');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>2,'alg'=>'C','txt'=>'Flag');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>19,'alg'=>'C','txt'=>'Script');
	$oDoc->setCol($aCol);
}